package com.dao.student.imp;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import org.apache.ibatis.cursor.Cursor;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import com.dao.student.MenuDao;
import com.po.Record;
import com.po.StudentChoose;
import com.po.StudentComment;
import com.po.TodayMenu;
import com.po.TomorrowMenu;

@Repository
public class MenuDaoImp implements MenuDao {

	@Resource(name="sqlSessionTemplate")
	private SqlSessionTemplate sqlSessionTemplate;
	public SqlSessionTemplate getSqlSessionTemplate() {
		return sqlSessionTemplate;
	}

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}
	
	//初始化今日菜单
	@Override
	public List<TodayMenu> queryTodayMenuList(String date) {
		// TODO Auto-generated method stub
		List<TodayMenu> todayList = sqlSessionTemplate.selectList("queryTodayList", date);
		
		return todayList;
	}
	//添加今日菜单评论
	@Override
	public int addStudentComment(StudentComment stuComment) {
		int result = sqlSessionTemplate.insert("addStudentComment", stuComment);
	//	int result1 = sqlSessionTemplate.insert("addRecord", stuComment);
		return result;
	}
	
	//获取带评论的今日菜单
	@Override
	public List<TodayMenu> getTodayMenuList(String date) {
		System.out.println(date);
		// TODO Auto-generated method stub
		List<TodayMenu> list = sqlSessionTemplate.selectList("getTodayMenuList",date);
		for(TodayMenu i: list){
			System.out.println(i.getDish_id()+"..."+i.getDish_comment());
		}
		List<TodayMenu> tempList = new ArrayList<TodayMenu>();
		  float sum_score = 0;
			for(TodayMenu i: list){
				int count=1;
				boolean state = false;
				for(TodayMenu j: tempList){
					if(j.getDish_id().equals(i.getDish_id())){
					    sum_score = j.getDish_comment();
						sum_score += i.getDish_comment();
						count = count + 1;
						float aver_score0 = sum_score/count;	
						System.out.println("average:"+aver_score0);
						int aver_score = new BigDecimal(aver_score0).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();	
						System.out.println("average1:"+aver_score);
						j.setDish_comment(aver_score);
						state = true;
					}
				}
				if(!state){
					tempList.add(i);  
				}
			}	
		return tempList;
	}
	
	//加载明日菜单
	@Override
	public List<TomorrowMenu> getTomorrowMenuList(String date) {
		// TODO Auto-generated method stub
		List<TomorrowMenu> list = sqlSessionTemplate.selectList("getTomorrowMenuList",date);
		return list;
	}
	
	//获取有点餐人数的明日菜单
	@Override
	public int updateTomorrwoMenuList(StudentChoose stuChoose) {
		return sqlSessionTemplate.update("updateTomorrowMenuList", stuChoose);
	}
	
	//记录已评价或已点餐studentID
	@Override
	public int changeRecord(Record record) {
		int result = 0;
		Record item = sqlSessionTemplate.selectOne("queryRecord", record);
		if(item != null){
			int today_state = item.getComment_state();
			int tomorrow_state = item.getChoose_state();
			if(today_state == 1){
				record.setComment_state(today_state);	
			}
			if(tomorrow_state == 1){
				record.setChoose_state(tomorrow_state);	
			}
			result = sqlSessionTemplate.update("updateRecord", record);
		}
		else{
			result = sqlSessionTemplate.insert("insertRecord", record);
		}
		return result;
	}

	//查询该ID是否评价或点餐过
	@Override
	public Record queryRecord(Record record) {
		// TODO Auto-generated method stub
		Record result = sqlSessionTemplate.selectOne("queryRecord", record);
		if(result != null){
			return result;
		}
		else{
			return null;
		}	
	}

	


}
